package io.renren.service;

import io.renren.dao.SysGeneratorDao;
import io.renren.dao.SysMenuDao;
import io.renren.entity.SysMenuEntity;
import io.renren.utils.GenUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午3:33:38
 */
@Service
public class SysGeneratorService {
	@Autowired
	private SysGeneratorDao sysGeneratorDao;

	@Autowired
	private SysMenuDao sysMenuDao;

	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		return sysGeneratorDao.queryList(map);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysGeneratorDao.queryTotal(map);
	}

	public Map<String, String> queryTable(String tableName) {
		return sysGeneratorDao.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return sysGeneratorDao.queryColumns(tableName);
	}

	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode(table, columns, zip);

			Configuration config = GenUtils.getConfig();
			if(config.getBoolean("autoImportMenu")){
				insertMenu(table.get("tableName"),table.get("tableComment"));
			}
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	private void insertMenu(String tableName,String tableComment){
		Configuration config = GenUtils.getConfig();
		String className = GenUtils.tableToJava(tableName, config.getString("tablePrefix"));

		String pathName = className.toLowerCase();
		String moduleName = config.getString("moduleName");

		SysMenuEntity parent = new SysMenuEntity();
		parent.setParentId(1L);
		parent.setName(tableComment);

		String url = "modules/" + moduleName + "/" + pathName + ".html";
		parent.setUrl(url);

		parent.setType(1);
		parent.setIcon("fa fa-file-code-o");
		parent.setOrderNum(6);

		sysMenuDao.insert(parent);
		long id = parent.getMenuId();

		SysMenuEntity button = new SysMenuEntity();
		button.setParentId(id);
		button.setType(2);
		button.setOrderNum(6);

		String prefix = moduleName + ":" + pathName + ":";

		button.setName("查看");
		String list = prefix + "list";
		String info = prefix + "info";
		button.setPerms(list + "," + info);

		sysMenuDao.insert(button);

		button.setName("新增");
		String save = prefix + "save";
		button.setPerms(save);

		sysMenuDao.insert(button);

		button.setName("修改");
		String update = prefix + "update";
		button.setPerms(update);

		sysMenuDao.insert(button);

		button.setName("删除");
		String delete = prefix + "delete";
		button.setPerms(delete);

		sysMenuDao.insert(button);
	}
}
